//
// @Author: Zhenwei Xiao
// @Description:
// @Version: 1.0.0
// @Date: 2021/4/7 下午3:03
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const linkService = require("./LinkService")
const encoding = require("../encoding/Encoder")
const block = require("../encoding/Block")
const LpPacket = require("../packet/LpPacket")
const buffer2arraybuffer = require("../common/buffer2arraybuffer");

class Transport  {
    constructor() {
        this.localAddr = ""
        this.remoteAddr = ""
        this.localUri  = ""
        this.remoteUri = ""
        this.linkService = new linkService.LinkService()
    }
    // TODO 虽然第一个版本可能用不到linkService分包与合包功能，
    // 但以后可能需要客户端也能直接发出用以太网封装的MIN网络包，所以还是留着linkService
    Close() {
        try {
            this.linkService.transport.conn[0].disconnect()
            this.linkService.transport.conn[1].disconnect()
        }catch (err){
            // log.Println(err)
            throw err
        }
    }
    //
    // @Description: 从[]byte中解析出LpPacket
    // @receiver e
    // @param buf []byte
    // @return *packet.LpPacket	解析出的包
    // @return error		解析失败错误
    //
    parseByteArray2LpPacket(buf) {
        var blocks
        try {
            // buf.buffer内存越界 获取了不正确的数据
            blocks = block.createBlockByBuffer(buffer2arraybuffer(buf), true)
        }catch (err){
            // log.Println(err)
            return err
        }
        if (!blocks.isValid()) {
            // log.Println("recv packet from face invalid")
            return err
        }
        var lpPacket = new LpPacket()
        try{
            lpPacket.wireDecode(blocks)
        }catch (err){
            // log.Println("parse to lpPacket error")
            return err
        }
        return lpPacket
    }
    //
    // @Description: 	将lpPacket编码成byte数组
    // @receiver t
    // @param lpPacket *packet.LpPacket
    // @return int     编码后byte数组的长度
    // @return []byte	编码得到的byte数组
    //
    encodeLpPacket2ByteArray(lpPacket) {
        var encoder = new encoding.Encoder
        encoder.encoderReset(encoding.MaxPacketSize, 0)
        var encodeBufLen
        try {
            encodeBufLen = lpPacket.wireEncode(encoder)
        }catch (err){
            // log.Println(err)
            return [-1, null]
        }
        var encodeBuf
        try {
            encodeBuf = encoder.getBuffer()
        }catch (err){
            // log.Println(err)
            return [-1, null]
        }
        return [encodeBufLen, encodeBuf]
    }

    //
    // @Description:
    // @receiver t
    // @return string
    //
    GetRemoteUri() {
        return this.remoteUri
    }

    //
    // @Description:
    // @receiver t
    // @return string
    //
    GetLocalUri() {
        return this.localUri
    }

    //
    // @Description:
    // @receiver t
    // @return string
    //
    GetRemoteAddr() {
        return this.remoteAddr
    }

    //
    // @Description:
    // @receiver t
    // @return string
    //
    GetLocalAddr() {
        return this.localAddr
    }
}

module.exports = {
    Transport: Transport
}
